/*
 * Famon (http://code.google.com/p/famon/)
 * Copyright (c) 2010 Carl Crous.
 *
 * This file is part of Famon.
 *
 * Famon is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Famon is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Famon.  If not, see <http://www.gnu.org/licenses/>.
 */

package za.org.foobar.famon.frontend.client.util;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.smartgwt.client.widgets.layout.Layout;
import org.gwtportlets.portlet.client.ui.LayoutPanel;
import za.org.foobar.famon.frontend.client.Famon;

/**
 *
 */
public class ClientUtil {
    public static final String NONE = "(none)";
    public static final String ALL = "(all)";
    public static final String YES = "Yes";
    public static final String NO = "No";

    public static boolean isNullOrNone(String value) {
        return value == null || NONE.equals(value);
    }

    public static boolean isNullNoneOrEmpty(String value) {
        return value == null || NONE.equals(value) || value.length() == 0;
    }

    public static boolean isNullOrAll(String value) {
        return value == null || ALL.equals(value);
    }

    public static String noneIfNull(String value) {
        if (value == null) {
            return NONE;
        }
        return value;
    }

    public static boolean isIE7() {
        return getUserAgent().equals("ie7");
    }

    public static boolean isIE() {
        return getUserAgent().startsWith("ie");
    }

    /**
     * Figure out what browser is running.
     */
    public static native String getUserAgent() /*-{
        try {
            if (window.opera) {
                return 'opera';
            }
            var ua = navigator.userAgent.toLowerCase();
            if (ua.indexOf('webkit' ) != -1) {
                return 'safari';
            }
            if (ua.indexOf('msie 6.0') != -1) {
                return 'ie6';
            }
            if (ua.indexOf('msie 7.0') != -1) {
                return 'ie7';
            }
            if (ua.indexOf('gecko') != -1) {
                return 'gecko';
            }
            return 'unknown';
        } catch (e) {
            return 'unknown'
        }
    }-*/ ;

    public static native String getLocation() /*-{
        try {
            if (self.parent.frames.length != 0) {
                return self.parent.location.href;
            }
        }
        catch (Exception) {}
        return document.location.href;
    }-*/;


    public static boolean falseIfNull(Boolean b) {
        return b != null && b;
    }

    public static boolean checkIsAdmin(Widget layout) {
        if (!Famon.getInstance().getPage().security.isAdmin) {
            HTML widget = new HTML("You do not have access to view this page");
            if (layout instanceof Layout) {
                ((Layout)layout).addMember(widget);
            } else if (layout instanceof LayoutPanel) {
                ((LayoutPanel)layout).add(widget);
            } else {
                Window.alert(widget.getHTML());
            }
            return false;
        }
        return true;
    }
}
